﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using Aliyeye.Car.Net.Utility;

namespace Aliyeye.Car.Net.Dal
{
    public class Repair
    {
        public Repair() { }
        public int Add(Model.Repair model)
        {
            //SqlParameter[] parameters = {
            //        new SqlParameter("@Id", SqlDbType.Int,4),
            //        new SqlParameter("@Time", SqlDbType.DateTime),
            //        new SqlParameter("@Cents", SqlDbType.NVarChar,500),
            //        new SqlParameter("@Factory", SqlDbType.NVarChar,50),
            //        new SqlParameter("@CLf", SqlDbType.Decimal,9),
            //        new SqlParameter("@RGF", SqlDbType.Decimal,9),
            //        new SqlParameter("@OtherPrice", SqlDbType.Decimal,9),
            //        new SqlParameter("@Sum", SqlDbType.Decimal,9),
            //        new SqlParameter("@Marks", SqlDbType.NVarChar,500),
            //        new SqlParameter("@InputUserId", SqlDbType.Int,4),
            //        new SqlParameter("@InputTime", SqlDbType.DateTime),
            //        new SqlParameter("@Type", SqlDbType.NVarChar,50),new SqlParameter("@carCode",SqlDbType.NVarChar,50)};
            //parameters[0].Direction = ParameterDirection.Output;
            //parameters[1].Value = model.Time;
            //parameters[2].Value = model.Cents;
            //parameters[3].Value = model.Factory;
            //parameters[4].Value = model.CLf;
            //parameters[5].Value = model.RGF;
            //parameters[6].Value = model.OtherPrice;
            //parameters[7].Value = model.Sum;
            //parameters[8].Value = model.Marks;
            //parameters[9].Value = model.InputUserId;
            //parameters[10].Value = model.InputTime;
            //parameters[11].Value = model.Type;
            //parameters[12].Value = model.CarCode;
            //int roleId = SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure, "Repair_ADD", parameters);
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Repair(");
            strSql.Append("Time,Cents,Factory,CLf,RGF,OtherPrice,Sum,Marks,InputUserId,InputTime,Type,CarCode,isRemind)");
            strSql.Append(" values (");
            strSql.Append("@Time,@Cents,@Factory,@CLf,@RGF,@OtherPrice,@Sum,@Marks,@InputUserId,@InputTime,@Type,@CarCode,@isRemind)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@Time", SqlDbType.DateTime),
					new SqlParameter("@Cents", SqlDbType.NVarChar,500),
					new SqlParameter("@Factory", SqlDbType.NVarChar,50),
					new SqlParameter("@CLf", SqlDbType.Decimal,9),
					new SqlParameter("@RGF", SqlDbType.Decimal,9),
					new SqlParameter("@OtherPrice", SqlDbType.Decimal,9),
					new SqlParameter("@Sum", SqlDbType.Decimal,9),
					new SqlParameter("@Marks", SqlDbType.NVarChar,500),
					new SqlParameter("@InputUserId", SqlDbType.Int,4),
					new SqlParameter("@InputTime", SqlDbType.DateTime),
					new SqlParameter("@Type", SqlDbType.NVarChar,50),
					new SqlParameter("@CarCode", SqlDbType.NVarChar,50),
					new SqlParameter("@isRemind", SqlDbType.Bit,1)};
            parameters[0].Value = model.Time;
            parameters[1].Value = model.Cents;
            parameters[2].Value = model.Factory;
            parameters[3].Value = model.CLf;
            parameters[4].Value = model.RGF;
            parameters[5].Value = model.OtherPrice;
            parameters[6].Value = model.Sum;
            parameters[7].Value = model.Marks;
            parameters[8].Value = model.InputUserId;
            parameters[9].Value = model.InputTime;
            parameters[10].Value = model.Type;
            parameters[11].Value = model.CarCode;
            parameters[12].Value = model.isRemind;

            int roleId = SqlHelper.ExecuteNonQuery(CommandType.Text, strSql.ToString(), parameters);
            if (roleId > 0)
            {
                return roleId;
            }
            else
            {
                return 0;
            }
        }
        public bool Update(Model.Repair model)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4),
					new SqlParameter("@Time", SqlDbType.DateTime),
					new SqlParameter("@Cents", SqlDbType.NVarChar,500),
					new SqlParameter("@Factory", SqlDbType.NVarChar,50),
					new SqlParameter("@CLf", SqlDbType.Decimal,9),
					new SqlParameter("@RGF", SqlDbType.Decimal,9),
					new SqlParameter("@OtherPrice", SqlDbType.Decimal,9),
					new SqlParameter("@Sum", SqlDbType.Decimal,9),
					new SqlParameter("@Marks", SqlDbType.NVarChar,500),
					new SqlParameter("@InputUserId", SqlDbType.Int,4),
					new SqlParameter("@InputTime", SqlDbType.DateTime),
					new SqlParameter("@Type", SqlDbType.NVarChar,50),
                    new SqlParameter("@carCode",SqlDbType.NVarChar,50),
					new SqlParameter("@isRemind", SqlDbType.Bit,1)};
            parameters[0].Value = model.Id;
            parameters[1].Value = model.Time;
            parameters[2].Value = model.Cents;
            parameters[3].Value = model.Factory;
            parameters[4].Value = model.CLf;
            parameters[5].Value = model.RGF;
            parameters[6].Value = model.OtherPrice;
            parameters[7].Value = model.Sum;
            parameters[8].Value = model.Marks;
            parameters[9].Value = model.InputUserId;
            parameters[10].Value = model.InputTime;
            parameters[11].Value = model.Type;
            parameters[12].Value = model.CarCode;
            parameters[13].Value = model.isRemind;
            int intResult = SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure, "Repair_Update", parameters);

            if (intResult > 0)
                return true;
            else
                return false;
        }

        /// <summary>
        /// 还车结算时，统计车辆维修价格总计
        /// </summary>
        /// <param name="carCode"></param>
        /// <param name="beginTime"></param>
        /// <param name="endTime"></param>
        /// <returns></returns>
        public decimal GetSumPriceByCarCode(string carCode, DateTime beginTime, DateTime endTime)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@CarCode", SqlDbType.NVarChar,50),
					new SqlParameter("@TimeBegin", SqlDbType.DateTime),
					new SqlParameter("@TimeEnd", SqlDbType.DateTime)
                             };

            parameters[0].Value = carCode;
            parameters[1].Value = beginTime;
            parameters[2].Value = endTime.AddSeconds(1);

            decimal result = 0;
            try
            {
                object obj = SqlHelper.ExecuteScalar(CommandType.StoredProcedure, "Repair_SumPrice", parameters);
                if (obj.ToString() != "")
                    result = Convert.ToDecimal(obj);
            }
            catch (Exception ex)
            {
            }
            return result;
        }

        public bool Delete(int Id)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)};
            parameters[0].Value = Id;
            int intResult = SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure, "Repair_Delete", parameters);

            if (intResult > 0)
                return true;
            else
                return false;
        }
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.Repair GetModel(int Id)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)};
            parameters[0].Value = Id;

            Model.Repair model = new Model.Repair();
            DataSet ds = SqlHelper.GetDataFormDataSet(CommandType.StoredProcedure, "Repair_GetModel", parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["Id"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
                }

                if (ds.Tables[0].Rows[0]["Time"].ToString() != "")
                {
                    model.Time = DateTime.Parse(ds.Tables[0].Rows[0]["Time"].ToString());
                }
                model.Cents = ds.Tables[0].Rows[0]["Cents"].ToString();
                model.Factory = ds.Tables[0].Rows[0]["Factory"].ToString();
                if (ds.Tables[0].Rows[0]["CLf"].ToString() != "")
                {
                    model.CLf = decimal.Parse(ds.Tables[0].Rows[0]["CLf"].ToString());
                }
                if (ds.Tables[0].Rows[0]["RGF"].ToString() != "")
                {
                    model.RGF = decimal.Parse(ds.Tables[0].Rows[0]["RGF"].ToString());
                }
                if (ds.Tables[0].Rows[0]["OtherPrice"].ToString() != "")
                {
                    model.OtherPrice = decimal.Parse(ds.Tables[0].Rows[0]["OtherPrice"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Sum"].ToString() != "")
                {
                    model.Sum = decimal.Parse(ds.Tables[0].Rows[0]["Sum"].ToString());
                }
                model.Marks = ds.Tables[0].Rows[0]["Marks"].ToString();
                if (ds.Tables[0].Rows[0]["InputUserId"].ToString() != "")
                {
                    model.InputUserId = int.Parse(ds.Tables[0].Rows[0]["InputUserId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["InputTime"].ToString() != "")
                {
                    model.InputTime = DateTime.Parse(ds.Tables[0].Rows[0]["InputTime"].ToString());
                }
                model.Type = ds.Tables[0].Rows[0]["Type"].ToString();
                model.CarCode = ds.Tables[0].Rows[0]["carcode"].ToString();
                model.InputTrueName = ds.Tables[0].Rows[0]["TrueName"].ToString();
                if (ds.Tables[0].Rows[0]["isRemind"] == DBNull.Value)
                {
                    model.isRemind = false;
                }
                else
                {
                    model.isRemind = Convert.ToBoolean(ds.Tables[0].Rows[0]["isRemind"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Id,Time,Cents,Factory,CLf,RGF,OtherPrice,Sum,Marks,InputUserId,InputTime,Type,carcode,isRemind ");
            strSql.Append(" FROM Repair ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return SqlHelper.Query(strSql.ToString());
        }
    }
}
